package com.jnevision.dict.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.jnevision.dict.model.Dict;
import com.jnevision.dict.service.DictServiceImpl;

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-13
 */

@RestController
@RequestMapping("/dicts")
public class DictController extends ApiController {
    @Autowired
    private DictServiceImpl dictServiceImpl;

    /**
     * 请假的类型
     * 
     * @return
     */
    @GetMapping("/leave")
    public R<List<Dict>> getLeave() {
        return this.success(this.dictServiceImpl.list(new QueryWrapper<Dict>().eq("type", "leave").orderByAsc("sort")));
    }

    /**
     * 它得的类型
     * 
     * @return
     */
    @GetMapping("/extra")
    public R<List<Dict>> getExtra() {
        return this.success(this.dictServiceImpl.list(new QueryWrapper<Dict>().eq("type", "extra").orderByAsc("sort")));
    }

    /**
     * 职位
     * 
     * @return
     */
    @GetMapping("/job")
    public R<List<Dict>> getJob() {
        return this.success(this.dictServiceImpl.list(new QueryWrapper<Dict>().eq("type", "job").orderByAsc("sort")));
    }

    /**
     * 政治面貌
     * 
     * @return
     */
    @GetMapping("/political")
    public R<List<Dict>> getPolitical() {
        return this
            .success(this.dictServiceImpl.list(new QueryWrapper<Dict>().eq("type", "political").orderByAsc("sort")));
    }

    /**
     * 保障
     * 
     * @return
     */
    @GetMapping("/baozhang")
    public R<List<Dict>> getBaozhang() {
        return this
            .success(this.dictServiceImpl.list(new QueryWrapper<Dict>().eq("type", "baozhang").orderByAsc("sort")));
    }

    /**
     * 字典表公用的新增
     * 
     * @param dict
     * @return
     */
    @PostMapping
    public R<Boolean> add(@RequestBody Dict dict) {
        String value = this.dictServiceImpl
            .getOne(new QueryWrapper<Dict>().eq("type", dict.getType()).orderByDesc("value").last("limit 1"))
            .getValue();

        dict.setValue(String.valueOf(Integer.valueOf(value) + 1));
        return this.success(this.dictServiceImpl.save(dict));
    }

    /**
     * 字典表公用的修改
     * 
     * @param dict
     * @return
     */
    @PutMapping
    public R<Boolean> update(@RequestBody Dict dict) {
        return this.success(this.dictServiceImpl.updateById(dict));
    }
}
